package com.getword.mongodao.impl;

import com.getword.domain.Article;
import com.getword.mongodao.ArticleOperations;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;

import java.util.List;

public class ArticleRepositoryImpl implements ArticleOperations {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<Article> findAllPage(Example<Article> example, DBObject fields, Pageable pageable) {
        Query query = new BasicQuery(null, fields.toString());
        query.addCriteria((new Criteria()).alike(example));
        query.with(pageable);
        List<Article> list = mongoTemplate.find(query, example.getProbeType());
        return PageableExecutionUtils.getPage(list, pageable, () -> {
            return mongoTemplate.count(query, example.getProbeType());
        });
    }

    @Override
    public Page<Article> findByTitleAndKeywords(String title, String keywords, Pageable page) {
        // 指定字段
        BasicDBObject fieldsObject = new BasicDBObject();
        fieldsObject.put("title", 1);
        fieldsObject.put("releaseTime", 1);
        fieldsObject.put("outlineDetail", 1);
        fieldsObject.put("sentiment", 1);
        return null;
    }
}
